package com.alex.j2se.stream;

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created by gaojun on 2018/7/227.
 */
public class StreamOverview {

    public static List<Integer> list = Arrays.asList(1, 12, 2, 3, 7, 5, 7, 10, 11, 11, 13);

    @Test
    public void filter() {
        list.stream().filter((i) -> i%2 == 0).forEach(System.out::println);
    }

    @Test
    public void distinct() {
        list.stream().distinct().forEach(System.out :: println);
    }

    @Test
    public void skip() {
        list.stream().skip(3).forEach(System.out :: println);
        System.out.println("--------");
        list.stream().skip(20).forEach(System.out :: println);
    }

    @Test
    public void limit() {
        list.stream().limit(2).forEach(System.out :: println);
        System.out.println("--------");
        list.stream().limit(20).forEach(System.out :: println);
    }

    @Test
    public void map() {
        list.stream().map((i) -> i * 2).forEach(System.out :: println);
    }

    @Test
    public void flatMap() {
        Arrays.asList("java scala javascript", "spark flink kylin", "yarn mesos").stream().
                flatMap((i) -> Arrays.asList(i.split(" ")).stream()).forEach(System.out :: println);
    }

    @Test
    public void sorted() {
        list.stream().sorted().forEach(System.out :: println);
    }

    @Test
    public void anyMatch() {
        System.out.println(Arrays.asList(1, 3, 5).stream().anyMatch(i -> i%2 == 0));
        System.out.println(Arrays.asList(1, 3, 5, 2).stream().anyMatch(i -> i%2 == 0));
    }

    @Test
    public void noneMatch() {
        System.out.println(Arrays.asList(1, 3, 5).stream().noneMatch(i -> i%2 == 0));
        System.out.println(Arrays.asList(1, 3, 5, 2).stream().noneMatch(i -> i%2 == 0));
    }

    @Test
    public void allMatch() {
        System.out.println(Arrays.asList(1, 3, 5).stream().allMatch(i -> i%2 == 0));
        System.out.println(Arrays.asList(0, 2, 4).stream().allMatch(i -> i%2 == 0));
    }

    @Test
    public void findAny() {
        System.out.println(list.stream().filter(i -> i%2 == 0).findAny().get());
        Arrays.asList(1, 3, 5, 2).stream().filter(i -> i%2 == 0).findAny().ifPresent(System.out :: println);
    }

    @Test
    public void findFirst() {
        System.out.println(list.stream().filter(i -> i%2 == 0).findFirst().get());
    }

    @Test
    public void count() {
        System.out.println(list.stream().filter(i -> i%2 == 0).count());
    }

    @Test
    public void collect() {
        list.stream().sorted(Collections.reverseOrder()).limit(3).forEach(System.out :: println);
        System.out.println("------");
        System.out.println(list.stream().sorted(Collections.reverseOrder()).limit(3).collect(Collectors.averagingInt(i -> i)).intValue());
    }

    @Test
    public void reduce() {
        list.stream().reduce(Integer :: max).ifPresent(System.out::println);
        System.out.println(list.stream().reduce(1, (a,b) -> a * b).intValue());
    }

    @Test
    public void testOption() {
        Assert.assertEquals(false, Arrays.asList("java scala javascript", "spark flink kylin", "yarn mesos").stream().filter(f -> "aa".equals(f)).findFirst().isPresent());
    }
}
